package br.edu.sistemaacademico;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.Historico;
import br.edu.sistemaacademico.pojo.Matricula;
import br.edu.sistemaacademico.pojo.Turma;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;


public class frmAluno extends FrmModelo{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	StateHandler stateAlterarExcuir = new StateHandler(false);
	Aluno aluno;
	Historico historico;
	Turma turma;
    List<Matricula> matriculas;
    List<Historico> extra;
    public List<Object> campos = new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    TabelaModelo tabModel;

        public frmAluno(java.awt.Frame parent, boolean modal, Aluno aluno) {
        super(parent, modal);
        tamanho.add("50");
        campos.add("codigo");
        campos.add("disciplina");
        campos.add("periodo");
        campos.add("professor");
        campos.add("curso");
        campos.add("situacao");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        this.aluno = aluno;
        setTitle("Aluno");
     //   setButtonAction(getJbtnGravar(), "matricula");
        getbtnVoltar().addActionListener(listener);
        getbtnVoltar().setActionCommand("voltar");
        getbtnExcluir().setActionCommand("excluir");
        getbtnExcluir().addActionListener(listener);
        getbtnAlterar().setActionCommand("alterar");
        getbtnAlterar().addActionListener(listener);
        getJbtnGravar().addActionListener(listener);
        getJbtnGravar().setActionCommand("matricula");
        setButtons();
        setStateAlteraExcuir();
        atualiza();
        setVisible(true);
    }


    public void Observer(ActionEvent evt, String operacao) {
        //frmCadastroCliente cc = new frmCadastroCliente();
        //addActionListener(ps);

    }

    public void setButtons(){
        JButton btn1 = getJbtnGravar();
        JButton btn2 = getbtnAlterar();
        JButton btn3 = getbtnExcluir();
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();
        btn1.setText("Matricula");
        btn3.setText("cancelar");
        btn4.setText("Historico");
        btn5.setVisible(false);
        btn6.setVisible(false);
    }
    public void atualiza(){
    	((DefaultTableModel) getJTable().getModel()).setNumRows(0);
		getJTable().updateUI();
		matriculas = (List) ClientCommand.carregar("MatriculaService","aluno="+aluno.getId(),"");
		for(int ind =0 ;ind<matriculas.size();ind++){
			if(matriculas.get(ind).getId()!=null)
		extra = (List) ClientCommand.carregar("HistoricoService","matricula="+matriculas.get(ind).getId(),"");
    	for(int i = 0; i < extra.size();i++ ){
    		valores = new LinkedList<Object>();
    		valores.add(extra.get(i).getTurma().getId());
    		valores.add(extra.get(i).getTurma().getClasse().getDisicplina().getNome());
    		valores.add(extra.get(i).getTurma().getPeriodo());
    		valores.add(extra.get(i).getTurma().getProfessor().getNome());
    		valores.add(extra.get(i).getTurma().getClasse().getCurso().getNome());
    		tabModel.setValores(valores);
    		valores.clear();
    	}
		}
    }
    ActionListener listener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			//if(e.getActionCommand().equals("excluir"));
				//professorDao.remover(o);
			if(e.getActionCommand().equals("voltar")){
			setVisible(false);
				return;
			}else if(e.getActionCommand().equals("turma")){
				FrmModelo frmT = new frmTurma(new Frame(), true);
			}else if(e.getActionCommand().equals("excluir"))
				remover();
			else if(e.getActionCommand().equals("alterar")){
				 new frmAtualizaAluno(aluno);
				 atualiza();
			}else if(e.getActionCommand().equals("matricula")){
				 new frmMatricula(aluno);
				 atualiza();
			}
		}
	};
    ListSelectionListener list = new ListSelectionListener() {
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			try{
				if(getJTable().getSelectedRow()>=0){
			historico = getSelectedHistorico(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
			stateAlterarExcuir.change(true);
				}
			}catch(ArrayIndexOutOfBoundsException ea){
				historico = null;
				stateAlterarExcuir.change(false);
			}
		}
	};	
	
	public Historico getSelectedHistorico(Long codigo){
		return (Historico) ClientCommand.carregaObjeto("HistoricoService","turma="+codigo,"");
	}
	public void remover(){
		ClientCommand.deletar("HistoricoService",historico);
		atualiza();
	}
    public void setStateAlteraExcuir(){
    	stateAlterarExcuir.addComponent(getbtnAlterar());
    	stateAlterarExcuir.addComponent(getbtnExcluir());
    }
}
